import pyautogui as pg
import os
import time
import pyperclip
import requests
import json
import psutil
import sys
import threading
import win32gui
import win32con
import win32clipboard as w
import win32api
from pywinauto.application import Application
import win32com
import win32process
import ctypes
import shutil



hwnd_title = dict()

def get_all_hwnd(hwnd,mouse):
    if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd):
        hwnd_title.update({hwnd:win32gui.GetWindowText(hwnd)})
# path1 = 'c:\gloden_files\全国接口测试-深圳（1）.GBQ6'
# path2 = 'C:\gloden_files2'

def init_export_golden(handle_path):
    windows_hwnd = None
    print('start')
    while True:
        if not windows_hwnd:
            win32gui.EnumWindows(get_all_hwnd, 0)   
            for h,t in hwnd_title.items():
                if "- 投标管理 - 广联达预算计价" in t:
                # print(h, t)
                    windows_hwnd = h
            time.sleep(1)
            pg.hotkey("enter")
        else :
            break
        # 获取屏幕分辨率  
    print('end')
    screen_width = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)
    screen_height = win32api.GetSystemMetrics(win32con.SM_CYSCREEN)

    # 获取窗口当前位置和大小  
    left, top, right, bottom = win32gui.GetWindowRect(windows_hwnd)
    window_width = right - left  
    window_height = bottom - top  

    # 计算新位置  
    new_left = (screen_width - window_width) // 2  
    new_top = (screen_height - window_height) // 2  

    # 移动窗口  
    win32gui.SetWindowPos(windows_hwnd, win32con.HWND_TOP, new_left, new_top, 1280, 760, 0)

    left, top, right, bottom = win32gui.GetWindowRect(windows_hwnd)

    print(str(left) + " " + str(top) + " " + str(right) + " " + str(bottom))
    pg.click((right - left) * (1 * (1.75 / 13)) + left, (bottom - top) * ((1.0 / 20) * 1) + top)
    time.sleep(0.5)
    pg.click((right - left) * (1 * (1.75 / 18)) + left, (bottom - top) * ((1.0 / 10) * 1) + top)
    time.sleep(0.5)
    pg.hotkey("esc")
    time.sleep(0.5)
    pg.hotkey("left")
    pg.hotkey("space")
    pg.typewrite(handle_path)
    time.sleep(0.5)
    pg.hotkey("enter")
    pg.hotkey("enter")
    pg.hotkey("tab")
    pg.typewrite("1")
    time.sleep(1)
    pg.hotkey("enter")

# 获取当前激活的窗口句柄

# init_export_golden('C:\\gloden_files')
tips = win32gui.FindWindow(None, "提示")
app = Application(backend='uia').connect(handle=tips)
dlg = app.window(title=win32gui.GetWindowText(tips))  
# print(dlg.print_ctrl_ids())
static_control = dlg.child_window(control_type="Text", found_index=3)  # found_index可能需要根据实际情况调整  
text = static_control.window_text()
